<HTML> 
<HEAD> 
	<TITLE>3rd Party ROBOTC Drivers</TITLE> 
<link href="tabs.css" rel="stylesheet" type="text/css"/> 
<link href="doxygen.css" rel="stylesheet" type="text/css"/> 
</HEAD> 
 
<BODY bgcolor="#ffffff" link="#000000" vlink="#000000"> 
<table width="100%" bgcolor="navy" cellspacing=0 cellpadding=1 border=0> 
<tr><td><table width="100%" bgcolor="#EEEEDD" cellspacing=0 cellpadding=3 border=0> 
<tr> 
    <td width="33%" align="left"> <img src="images/LOGO_NXT.gif" width=266 height=44 border="0" alt="Mindstorms"></td> 
    <td><b><font size="+3" color="navy">3rd Party ROBOTC Drivers</font></b></td> 
    <td align="right"> <img src="images/logo.png" width=44 height=44 border="0" alt="RobotC"></td>     
</tr> 
  <tr bgcolor="#cccc99" align="right"> 
          <td colspan=3> <font face="arial" size="-1"> [<a href=main.html>Home</a>] [<a target=_top href="https://sourceforge.net/projects/rdpartyrobotcdr/">Download</a>] [<a target=_top href="http://apps.sourceforge.net/mantisbt/rdpartyrobotcdr/my_view_page.php">Submit a bug/suggestion</a>]  [<a target=_top href="http://www.robotc.net/forums/">ROBOTC Forums</a>] [<a target=_top href="http://botbench.com">Blog</a>] [<a target=_top href="http://sourceforge.net/donate/index.php?group_id=257238">Support this project</a>]</font> </td> 
</tr> 
</table></td></tr> 
</table> 
<!-- Generated by Doxygen 1.7.2 -->
<div class="header">
  <div class="headertitle">
<h1>lego-temp-test1.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="preprocessor">#pragma config(Sensor, S1,     LEGOTMP,             sensorI2CCustom)</span>
<span class="preprocessor"></span><span class="comment">//*!!Code automatically generated by &#39;ROBOTC&#39; configuration wizard               !!*//</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * lego-temp.h provides an API for the Lego Temperature Sensor.  This program</span>
<span class="comment"> * demonstrates how to use that API to use the sensor in continuous mode.</span>
<span class="comment"> *</span>
<span class="comment"> * Changelog:</span>
<span class="comment"> * - 0.1: Initial release</span>
<span class="comment"> *</span>
<span class="comment"> * Credits:</span>
<span class="comment"> * - Big thanks to Sylvain Cacheux for writing the initial drivers.</span>
<span class="comment"> *</span>
<span class="comment"> * License: You may use this code as you wish, provided you give credit where it&#39;s due.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. </span>
<span class="comment"></span>
<span class="comment"> * Xander Soldaat (xander_at_botbench.com)</span>
<span class="comment"> * 16 february 2010</span>
<span class="comment"> * version 0.1</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &quot;<a class="code" href="lego-temp_8h.html" title="RobotC New Temperature Sensor Driver.">drivers/lego-temp.h</a>&quot;</span>

<span class="keywordtype">void</span> accuracyToString(<a class="code" href="group__legotmp.html#ga48bff0e4f0b49f60c3bca0de82fd2b6c">tLEGOTMPAccuracy</a> _accuracy, <span class="keywordtype">string</span> &amp;text) {
  strcpy(text, <span class="stringliteral">&quot;&quot;</span>);
  <span class="keywordflow">switch</span> (_accuracy) {
    <span class="keywordflow">case</span> <a name="a0"></a><a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>:
      text = <span class="stringliteral">&quot;A_MIN&quot;</span>;
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> <a name="a1"></a><a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafad5e4ea93da5b2f842b8f40c0136487">A_MEAN1</a>:
      text = <span class="stringliteral">&quot;A_MEAN1&quot;</span>;
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> <a name="a2"></a><a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafced960a9964f577c41af4bf5d65586f">A_MEAN2</a>:
      text = <span class="stringliteral">&quot;A_MEAN2&quot;</span>;
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> <a name="a3"></a><a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6ca4bd6597215b5560c8e6e6b0c3cf862ea">A_MAX</a>:
      text = <span class="stringliteral">&quot;A_MAX&quot;</span>;
      <span class="keywordflow">break</span>;
  }
}


task main() {
  <span class="keywordtype">float</span> temp;
  <a class="code" href="group__legotmp.html#ga48bff0e4f0b49f60c3bca0de82fd2b6c">tLEGOTMPAccuracy</a> accuracy;
  <span class="keywordtype">string</span> strAcc;

  nxtDisplayCenteredTextLine(0, <span class="stringliteral">&quot;LEGO&quot;</span>);
  nxtDisplayCenteredBigTextLine(1, <span class="stringliteral">&quot;Temp&quot;</span>);
  nxtDisplayCenteredTextLine(3, <span class="stringliteral">&quot;Test 1&quot;</span>);
  nxtDisplayCenteredTextLine(5, <span class="stringliteral">&quot;Connect sensor&quot;</span>);
  nxtDisplayCenteredTextLine(6, <span class="stringliteral">&quot;to S1&quot;</span>);
  wait1Msec(2000);
  eraseDisplay();


  <span class="comment">// Setup the sensor for continuous mode</span>
  <a name="a4"></a><a class="code" href="group__legotmp.html#ga301003d7fb24757bc8ba6a7a8a1ab7a4">LEGOTMPsetContinuous</a>(LEGOTMP);

  <span class="comment">//setting minimum accuracy</span>
  accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>;
  <span class="keywordflow">if</span> (!<a name="a5"></a><a class="code" href="group__legotmp.html#ga80095d3a4a00b5217f397fa29f62f484">LEGOTMPsetAccuracy</a>(LEGOTMP, accuracy)) {
    nxtDisplayTextLine(0, <span class="stringliteral">&quot;Error setAccuracy&quot;</span>);
    wait1Msec(5000);
    StopAllTasks();
  }

  <span class="comment">//reads the current accuracy of the sensor</span>
  <span class="keywordflow">if</span> (!<a name="a6"></a><a class="code" href="group__legotmp.html#ga251e764f7f9950610770a3500eefb0ef">LEGOTMPreadAccuracy</a>(LEGOTMP, accuracy)) {
    nxtDisplayTextLine(0, <span class="stringliteral">&quot;Error readAccuracy&quot;</span>);
    wait1Msec(5000);
    StopAllTasks();
  }

  accuracyToString(accuracy, strAcc);
  nxtDisplayTextLine(0, <span class="stringliteral">&quot;Accuracy: %s&quot;</span>, strAcc);

  <span class="comment">//loop to read temp</span>
  <span class="keywordflow">while</span> (<span class="keyword">true</span>) {
    <span class="keywordflow">switch</span>(nNxtButtonPressed) {
      <span class="comment">// If the left button is pressed, decrease the accuracy</span>
      <span class="keywordflow">case</span> kLeftButton:
        <span class="keywordflow">switch</span>(accuracy) {
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>:   accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6ca4bd6597215b5560c8e6e6b0c3cf862ea">A_MAX</a>;   <span class="keywordflow">break</span>;
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafad5e4ea93da5b2f842b8f40c0136487">A_MEAN1</a>: accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>;   <span class="keywordflow">break</span>;
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafced960a9964f577c41af4bf5d65586f">A_MEAN2</a>: accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafad5e4ea93da5b2f842b8f40c0136487">A_MEAN1</a>; <span class="keywordflow">break</span>;
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6ca4bd6597215b5560c8e6e6b0c3cf862ea">A_MAX</a>:   accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafced960a9964f577c41af4bf5d65586f">A_MEAN2</a>; <span class="keywordflow">break</span>;
        }
        <span class="keywordflow">if</span> (!<a class="code" href="group__legotmp.html#ga80095d3a4a00b5217f397fa29f62f484">LEGOTMPsetAccuracy</a>(LEGOTMP, accuracy)) {
          nxtDisplayTextLine(0, <span class="stringliteral">&quot;Error setAccuracy&quot;</span>);
          wait1Msec(5000);
          StopAllTasks();
        }
        accuracyToString(accuracy, strAcc);
        nxtDisplayTextLine(0, <span class="stringliteral">&quot;Accuracy: %s&quot;</span>, strAcc);

        <span class="comment">// debounce the button</span>
        <span class="keywordflow">while</span> (nNxtButtonPressed != kNoButton) EndTimeSlice();
        <span class="keywordflow">break</span>;

      <span class="comment">// If the right button is pressed, increase the accuracy</span>
      <span class="keywordflow">case</span> kRightButton:
        <span class="keywordflow">switch</span>(accuracy) {
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>:   accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafad5e4ea93da5b2f842b8f40c0136487">A_MEAN1</a>; <span class="keywordflow">break</span>;
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafad5e4ea93da5b2f842b8f40c0136487">A_MEAN1</a>: accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafced960a9964f577c41af4bf5d65586f">A_MEAN2</a>; <span class="keywordflow">break</span>;
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafced960a9964f577c41af4bf5d65586f">A_MEAN2</a>: accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6ca4bd6597215b5560c8e6e6b0c3cf862ea">A_MAX</a>;   <span class="keywordflow">break</span>;
          <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6ca4bd6597215b5560c8e6e6b0c3cf862ea">A_MAX</a>:   accuracy = <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>;   <span class="keywordflow">break</span>;
        }
        <span class="keywordflow">if</span> (!<a class="code" href="group__legotmp.html#ga80095d3a4a00b5217f397fa29f62f484">LEGOTMPsetAccuracy</a>(LEGOTMP, accuracy)) {
          nxtDisplayTextLine(0, <span class="stringliteral">&quot;Error setAccuracy&quot;</span>);
          wait1Msec(5000);
          StopAllTasks();
        }
        accuracyToString(accuracy, strAcc);
        nxtDisplayTextLine(0, <span class="stringliteral">&quot;Accuracy: %s&quot;</span>, strAcc);

        <span class="comment">// debounce the button</span>
        <span class="keywordflow">while</span> (nNxtButtonPressed != kNoButton) EndTimeSlice();
        <span class="keywordflow">break</span>;
    }
    <span class="keywordflow">if</span> (!<a name="a7"></a><a class="code" href="group__legotmp.html#gab8ad53688664407e14adfc5a5576d5b3">LEGOTMPreadTemp</a>(LEGOTMP, temp)) {
      eraseDisplay();
      nxtDisplayTextLine(0, <span class="stringliteral">&quot;Temp reading pb&quot;</span>);
      wait10Msec(100);
      StopAllTasks();
    }

    nxtDisplayCenteredBigTextLine(2, <span class="stringliteral">&quot;Temp:&quot;</span>);
    <span class="keywordflow">switch</span>(accuracy) {
      <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cad88902fec96e801e3e6886338efdb8d0">A_MIN</a>:   nxtDisplayCenteredBigTextLine(4, <span class="stringliteral">&quot;%4.1f&quot;</span>, temp); <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafad5e4ea93da5b2f842b8f40c0136487">A_MEAN1</a>: nxtDisplayCenteredBigTextLine(4, <span class="stringliteral">&quot;%5.2f&quot;</span>, temp); <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6cafced960a9964f577c41af4bf5d65586f">A_MEAN2</a>: nxtDisplayCenteredBigTextLine(4, <span class="stringliteral">&quot;%6.3f&quot;</span>, temp); <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> <a class="code" href="group__legotmp.html#gga48bff0e4f0b49f60c3bca0de82fd2b6ca4bd6597215b5560c8e6e6b0c3cf862ea">A_MAX</a>:   nxtDisplayCenteredBigTextLine(4, <span class="stringliteral">&quot;%7.4f&quot;</span>, temp); <span class="keywordflow">break</span>;
    }
  }
}
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Mar 13 2013 19:56:42 for ROBOTC Drivers by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>
